<?	/*
	// File:	pdns.wcp.phps
	// Purpose:	powerdns domain manipulation
	// Author:	Peter Drake <peterdrake@241computers.com>
	// Updated by:  Steve Kondik <shade@chemlab.org> for PDNS
	*/
/* Moved pdns into module pdns.mod.phps
// Check to see if we are supposed to do anything
if ($type == "domain") {

	if ($data['action'] == "create" || $data['action'] == "update") {

		// These have to happen before switching databases
		// so they are selected early - if this is a creation.

		// Select hosts
		$query = "SELECT ip, host FROM domains WHERE (id='".$data['id']."' OR owner='".$data['id']."') AND domain = '".$data['domain']."' AND action != 'remove'";
		$dbp = mysql_query($query);

		// Select the old domain name??
		$query="SELECT domain FROM domains WHERE id='".$data['id']."' AND action != 'remove'";
		$oldomain = mysql_query($query);
		$olddom = @mysql_fetch_row($olddomain);
		$data['ip'] = explode('|',$data['ip']);
	}
	
	// Select PDNS database as defined in config
	if (!@mysql_select_db($cfg['pdns_db'])) {
		webcp_log(1, 0, "pdns.mod.phps", "Failed to select PowerDNS database: ".$cfg['pdns_db'],0 , $echo);
	} else {
		webcp_log(3, 0, "pdns.mod.phps", "PowerDNS Database selected: ".$cfg['pdns_db'],0 , $echo);
	}
	
	
	// DNS admin email:
	$dns_admin_email = str_replace("@", ".", $cfg['dns_email']);
	if (strrpos($dns_admin_email, ".") != strlen($dns_admin_email)) {
		$dns_admin_email .= ".";
	}
	
	//Create serial number
	$dns_serial = date("Ymd");
	
	// Make it unique
	$SQL = "SELECT MAX(id) as MaxID FROM domains";
	$res = mysql_query($SQL);
	
	if ($id = mysql_fetch_array($res) && mysql_num_rows($res)) {
		$dns_serial .= ($id["MaxID"] + 1);
	} else {
		$dns_serial .= date("His");
	}


switch($data["action"]) {
	case "suspend":
	case "unsuspend":
		// No action for suspend and unsuspend
		return;
		break;
	case "update":
	case "remove":
		// Select old SOA to work with
		$SQL = "SELECT id FROM domains WHERE name = '".$data["domain1"]."'";
		$res = mysql_query($SQL);
		if ($id = mysql_fetch_array($res)) {
			switch ($data['type']) {
				case "pointer":
				case "domain":
					// Delete old SOA
					$SQL = "DELETE FROM domains WHERE id = ".$id["id"];
					mysql_query($SQL);
			
					// Delete old RR
					$SQL = "DELETE FROM records WHERE domain_id = ".$id["id"];
					mysql_query($SQL);

					break;
				case "subdomain":
					// Delete subdomain
					$SQL = "DELETE FROM records WHERE domain_id = ".$id["id"]." AND name='".$data['host'].".".$data['domain']."'";
					mysql_query($SQL);
					break;
			}
		}
		if ($data["action"] == "remove") { break; }
	case "create":
		switch ($data['type']) {
			case "pointer":
			case "domain":
				// Select old SOA to work with
				$SQL = "SELECT id FROM domains WHERE origin = '".$olddom["domain"]."'";
				$res = mysql_query($SQL);
				if ($id = mysql_fetch_array($res) && mysql_num_rows($res)) {
					// Delete old SOA
					$SQL = "DELETE FROM domains WHERE id = ".$id["id"];
					mysql_query($SQL);
			
					// Delete old RR
					$SQL = "DELETE FROM records WHERE domain_id = ".$id["id"];
					mysql_query($SQL);
				}

				if (strrpos($cfg["dns_server1"], ".") != strlen($cfg["dns_server1"])) {
					$dns_server1 = $cfg["dns_server1"].".";
				} else {
					$dns_server1 = $cfg["dns_server1"];
				}

				// create SOA record
				$SQL = "INSERT INTO domains(name, type) values ('" . $data["domain"] . "', 'NATIVE')";
				mysql_query($SQL);
				$SOAID = mysql_insert_id();
				
				// create SOA record
				$SQL = "INSERT INTO records(domain_id, name, type, content, ttl, prio) values (" . $SOAID . ", '" . $data["domain"] . "', 'SOA', '" . $dns_server1 . " " . $dns_admin_email . " " . $dns_serial . " " . $cfg['dns_refresh'] . " " . $cfg['dns_retry'] . " " . $cfg['dns_expire'] . " " . $cfg['dns_ttl'] . "', '" . $cfg['dns_ttl'] . "', '0')";
				
				mysql_query($SQL);

				// create NS records
				for($i=1; $i<=4; $i++){
					if( !preg_match("/^$|none/", $cfg["dns_server".$i])){
						if (strrpos($cfg["dns_server".$i], ".") != strlen($cfg["dns_server".$i])) {
							$dns_server = $cfg["dns_server".$i].".";
						} else {
							$dns_server = $cfg["dns_server".$i];
						}
						$SQL = "INSERT INTO records (domain_id, name, type, content, ttl) VALUES(";
							$SQL .= $SOAID.",'" . $data["domain"] . "','NS','".$dns_server."',".$cfg["dns_ttl"].")";
						mysql_query($SQL);
					}
				}
		
				// create MX records
				for($i=1; $i<=4; $i++){
					if( !preg_match("/^$|none/", $cfg["mail_exchanger".$i])){
						if (strrpos($cfg["mail_exchanger".$i], ".") != strlen($cfg["mail_exchanger".$i])) {
							$mail_server = $cfg["mail_exchanger".$i].".";
						} else {
							$mail_server = $cfg["mail_exchanger".$i];
						}
						$SQL = "INSERT INTO records(domain_id, name, type, content, ttl, prio) VALUES(";
							$SQL .= $SOAID.",'" . $data["domain"] . "','MX','".$mail_server."',".$cfg["dns_ttl"].", ". ($i * 10) .")";
						mysql_query($SQL);
					}
				}
		
				// create A records for hosts
				while ($ddata = mysql_fetch_array($dbp)) {
					foreach($data['ip'] as $IP) {
						$SQL = "INSERT INTO records(domain_id, name, type, content, ttl) VALUES(";
							$SQL .= $SOAID.",'".$ddata["host"].".".$data['domain']."','A','".$IP."',".$cfg["dns_ttl"].")";
						mysql_query($SQL);
					}
				}

				// create A record for plain domain.ext if requested
				if($data['nohost'] == 'true'){
					foreach($data['ip'] as $IP) {
						$SQL = "INSERT INTO records(zone, name, type, content, ttl) VALUES(";
							$SQL .= $SOAID.",'".$data["domain"]."','A','".$IP."',".$cfg["dns_ttl"].")";
						mysql_query($SQL);
					}
				}
		
				// create A record for hostname aliases
				if (!empty($data['aliases'])) {
				$tmp = explode(" ",$data['aliases']);
					foreach($tmp as $alias){
						foreach($data['ip'] as $IP) {
							$SQL = "INSERT INTO records(domain_id, name, type, content, ttl) VALUES(";
								$SQL .= $SOAID.",'".$alias."','A','".$IP."',".$cfg["dns_ttl"].")";
							mysql_query($SQL);
						}
					}
				}
		
				// create A catchall record if requested
				if ($data['catchall'] == 'true'){
					foreach($data['ip'] as $IP) {
						$SQL = "INSERT INTO records(domain_id, name, type, content, ttl) VALUES(";
							$SQL .= $SOAID.",'*','A','".$IP."',".$cfg["dns_ttl"].")";
						mysql_query($SQL);
					}
				}
				break;

			case "subdomain":
				// Select SOA to use
				$SQL = "SELECT id FROM domains WHERE name = '".$data["domain"]."'";
				$res = mysql_query($SQL);

				if ($id = mysql_fetch_array($res)) {
					// Check if subdomain already exists
					$SQL = "SELECT id FROM records WHERE domain_id = '".$id["id"]."' AND name='".$data["host"]."'";
					$oldsub = mysql_query($SQL);
					if ($osub = mysql_fetch_array($oldsub)) {
						// Delete old subdomain
						$SQL = "DELETE FROM records WHERE id = ".$osub["id"];
						mysql_query($SQL);
					}

					// Create subdomain
					foreach($data['ip'] as $IP) {
						$SQL = "INSERT INTO records(domain_id, name, type, content, ttl) VALUES(";
							$SQL .= $id['id'].",'".$data["host"].".".$data['domain']."','A','".$IP."',".$cfg["dns_ttl"].")";
						mysql_query($SQL);
					}
				}
				break;
		}
	break;
		
}

	// Log completion
	webcp_log(2, 0, "pdns.wcp.phps", "PowerDNS entry for ".$data["domain"]." completed successfully. Type: ".$data['type'].". Action: ".$data['action'],0 , $echo);

	// Select webcp database again
	mysql_select_db($cfg['dbname']);

}
/*
?>
